<?php

/**
 * web端用信息
 * @return mixed
 */
function web_user_info()
{
    $user = web_user()->user();
    if ($user) {
        return $user->toArray();
    }
    return null;
}

/**
 * web端用
 * @return mixed
 */
function web_user()
{
    return guard('web');
}

/**
 * @param string $s_model
 * @return mixed
 */
function guard(string $s_model = '')
{
    return \Illuminate\Support\Facades\Auth::guard($s_model);
}

/**
 * 返回完整OSS路径
 * @param $value
 * @return string
 */
function oss_url($value)
{
    $url = 'http://' . env('OSS_BUCKET', 'yi-ming') . '.';
    $url .= str_replace('http://', '', env('OSS_ENDPOINT', 'http://oss-cn-hangzhou.aliyuncs.com')) . '/' . $value;

    return $url;
}

/**
 * 发送邮件
 * @param string $title 邮件标题
 * @param string $email 收件地址
 * @param string $content 邮件内容...
 * @return array
 */
function send_mail($title = '', $email = '', $content = '')
{
    if (empty($title) || empty($email) || empty($content)) {
        return hello_error('参数错误！');
    } else {
        if (!checkdnsrr(array_pop(explode("@", $email)), "MX")) {
            return hello_error('邮箱错误！');
        }
        \Illuminate\Support\Facades\Mail::raw($content, function ($message) use ($title, $email) {
            $message->to($email)->subject($title);
        });
        return hello_success('发送成功！');
    }
}

/**
 * 删除数组中值为null的字段
 * @param array $array
 * @return array
 */
function array_del_null(array $array)
{
    foreach ($array as $key => $value) {
        if (is_null($array[$key])) {
            array_pull($array, $key);
        }
    }
    return $array;
}

/**
 * 过滤数组
 * @param array $array
 * @return array
 */
function array_trim($array = [])
{
    $arr_data = [];
    foreach ($array as $key => $value) {
        if (isset($array[$key]) && (!is_null($value)) && (trim($value) !== '')) {
            if (is_string($value)) {
                $value = trim($value);
            }
            $arr_data[$key] = $value;
        }
    }
    return $arr_data;
}

/**
 * 过滤字符串
 * @param string $str
 * @return string
 */
function string_trim($str = '')
{
    if (is_string($str)) {
        if (isset($str) && (!is_null($str)) && (trim($str) !== '')) {
            $str = trim($str);
        } else {
            $str = '';
        }
    } else {
        $str = '';
    }
    return $str;
}

/**
 * 友好的时间显示 输入时间戳
 * @param $time
 * @param string $format
 * @return false|string
 */
function friendly_date($time, $format = 'Y-m-d')
{
    if (!$time) {
        return '';
    }
    $time = strtotime($time);
    $nowtime = time();
    if ($time > $nowtime) {
        return date($format, $time);
    }

    $z = date('z', $time);//当前的第几天
    $nowz = date('z', $nowtime);
    if ($z > $nowz) {
        $nowz += 365;
    }
    $diffz = $nowz - $z;//获取差异天
    $diffs = $nowtime - $time;//获取差异秒

    if ($diffz >= 7) {
        return date($format, $time);
    } elseif ($diffz >= 3) {
        return $diffz . '天前';
    } elseif ($diffz >= 2) {
        return '前天';
    } elseif ($diffz >= 1) {
        return '昨天';
    } elseif ($diffs >= 3600) {
        return floor($diffs / 3600) . '小时前';
    } elseif ($diffs >= 60) {
        return floor($diffs / 60) . '分钟前';
    } elseif ($diffs >= 10) {
        return $diffs . '秒前';
    } else {
        return '刚刚';
    }

}


